package com.soin.smc.web.controller;

import com.soin.smc.common.annotation.Log;
import com.soin.smc.common.core.controller.BaseController;
import com.soin.smc.common.core.domain.AjaxResult;
import com.soin.smc.common.enums.BusinessType;
import com.soin.smc.common.utils.poi.ExcelUtil;
import com.soin.smc.models.domain.District;
import com.soin.smc.models.service.DistrictService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 区划信息Controller
 *
 * @author adinlead
 * @date 2023-03-02
 */
@RestController
@RequestMapping("/api/district")
public class DistrictController extends BaseController {
    @Resource
    private DistrictService districtService;

    /**
     * 查询区划信息列表
     */
    @PreAuthorize("@ss.hasPermi('models:district:list')")
    @GetMapping("/list")
    public AjaxResult list(District district) {
        List<District> list = districtService.selectDistrictList(district);
        return success(list);
    }

    /**
     * 导出区划信息列表
     */
    @PreAuthorize("@ss.hasPermi('models:district:export')")
    @Log(title = "区划信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, District district) {
        List<District> list = districtService.selectDistrictList(district);
        ExcelUtil<District> util = new ExcelUtil<>(District.class);
        util.exportExcel(response, list, "区划信息数据");
    }

    /**
     * 获取区划信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('models:district:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(districtService.selectDistrictById(id));
    }

    /**
     * 新增区划信息
     */
    @PreAuthorize("@ss.hasPermi('models:district:add')")
    @Log(title = "区划信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody District district) {
        return toAjax(districtService.insertDistrict(district));
    }

    /**
     * 修改区划信息
     */
    @PreAuthorize("@ss.hasPermi('models:district:edit')")
    @Log(title = "区划信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody District district) {
        return toAjax(districtService.updateDistrict(district));
    }

    /**
     * 删除区划信息
     */
    @PreAuthorize("@ss.hasPermi('models:district:remove')")
    @Log(title = "区划信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(districtService.deleteDistrictByIds(ids));
    }
}
